"""
@Project ：python
@File    ：04-数据处理-生成折线图.py
@IDE     ：PyCharm
@Author  ：DongCha
@Date    ：2024/4/7 007 14:13
"""

# 数据在 ./数据/处理前/折线图数据 中
# 在线 JSON 处理：https://www.bejson.com/jsonviewernew/
# 原始数据格式：
# jsonp_1629350871167_29498(
#     {
#         "status": 0,
#         "msg": "success",
#         "data": [
#             {
#                 "name": "日本",
#                 "trend": {
#                     "updateDate": ["4.7", "4.8", "4.9"],
#                     "list": [
#                         {
#                             "name": "确诊",
#                             "data": [5480, 5916, 6725]
#                         },
#                         {
#                             "name": "治愈",
#                             "data": [5480, 5916, 6725]
#                         },
#                         {
#                             "name": "死亡",
#                             "data": [5480, 5916, 6725]
#                         },
#                         {
#                             "name": "新增确诊",
#                             "data": [5480, 5916, 6725]
#                         }
#                     ]
#                 }
#             }
#         ]
#     }
# );

import json
from pyecharts.charts import Line
from pyecharts.options import TitleOpts, DataZoomOpts, LabelOpts

date = ''
us_diagnose = ''  # 确诊
us_cure = ''  # 治愈
us_death = ''  # 死亡
us_add = ''  # 新增
jp_diagnose = ''
jp_cure = ''
jp_death = ''
jp_add = ''
in_diagnose = ''
in_cure = ''
in_death = ''
in_add = ''

# 处理美国数据
with (open("数据/美国.txt", "r", encoding="utf-8") as f):
    us_json = f.read()
    # 去掉开头
    us_json = us_json.replace("jsonp_1629344292311_69436(", "")
    # 去掉最后两个字符
    us_json = us_json[:-2]
    # JSON转为python字典
    us_diet = json.loads(us_json)
    # 获取 trend 层
    us_trend = us_diet['data'][0]['trend']
    # 获取 x 数据
    date = us_trend['updateDate'][:314]
    # 获取数据
    us_diagnose = us_trend['list'][0]['data'][:314]
    us_cure = us_trend['list'][1]['data'][:314]
    us_death = us_trend['list'][2]['data'][:314]
    us_add = us_trend['list'][3]['data'][:314]

# 处理日本数据
with (open("数据/日本.txt", "r", encoding="utf-8") as f):
    jp_json = f.read()
    # 去掉开头
    jp_json = jp_json.replace("jsonp_1629350871167_29498(", "")
    # 去掉最后两个字符
    jp_json = jp_json[:-2]
    # JSON转为python字典
    jp_diet = json.loads(jp_json)
    # 获取 trend 层
    jp_trend = jp_diet['data'][0]['trend']
    # 获取数据
    jp_diagnose = jp_trend['list'][0]['data'][:314]
    jp_cure = jp_trend['list'][1]['data'][:314]
    jp_death = jp_trend['list'][2]['data'][:314]
    jp_add = jp_trend['list'][3]['data'][:314]

# 处理印度数据
with (open("数据/印度.txt", "r", encoding="utf-8") as f):
    in_json = f.read()
    # 去掉开头
    in_json = in_json.replace("jsonp_1629350745930_63180(", "")
    # 去掉最后两个字符
    in_json = in_json[:-2]
    # JSON转为python字典
    in_diet = json.loads(in_json)
    # 获取 trend 层
    in_trend = in_diet['data'][0]['trend']
    # 获取数据
    in_diagnose = in_trend['list'][0]['data'][:314]
    in_cure = in_trend['list'][1]['data'][:314]
    in_death = in_trend['list'][2]['data'][:314]
    in_add = in_trend['list'][3]['data'][:314]

# 得到折线图对象
line = Line()
# 添加 x 轴数据
line.add_xaxis(date)
# 添加 y 轴数据，label_opts=LabelOpts(is_show=False) 设置系列属性，文本标签不可见
line.add_yaxis("美国确诊人数", us_diagnose, label_opts=LabelOpts(is_show=False))
line.add_yaxis("美国治愈人数", us_cure, label_opts=LabelOpts(is_show=False))
line.add_yaxis("美国死亡人数", us_death, label_opts=LabelOpts(is_show=False))
line.add_yaxis("美国新增人数", us_add, label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本确诊人数", jp_diagnose, label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本治愈人数", jp_cure, label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本死亡人数", jp_death, label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本新增人数", jp_add, label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度确诊人数", in_diagnose, label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度治愈人数", in_cure, label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度死亡人数", in_death, label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度新增人数", in_add, label_opts=LabelOpts(is_show=False))
# 设置图表全局属性
line.set_global_opts(
    title_opts=TitleOpts(True, "2020年疫情", pos_left="center", pos_bottom="1%"),
    datazoom_opts=DataZoomOpts(pos_left='center', is_show=True)
)
# 生成图表
line.render("./render/line.html")
